package com.ewhale.feitengguest.widget

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.drawable.ColorDrawable
import android.os.Handler
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.WindowManager
import androidx.core.widget.addTextChangedListener

import com.ewhale.feitengguest.R
import com.simga.library.activity.MBaseActivity
import com.simga.library.utils.KeyBoardUtils
import com.simga.library.utils.LogUtil
import com.simga.library.utils.glide.GlideUtil
import kotlinx.android.synthetic.main.dialog_increase_number.*
import kotlinx.android.synthetic.main.dialog_increase_prcie.*
import kotlinx.android.synthetic.main.dialog_increase_prcie.btn_commit
import kotlinx.android.synthetic.main.dialog_increase_prcie.et_name
import kotlinx.android.synthetic.main.dialog_increase_prcie.iv_close
import kotlinx.android.synthetic.main.dialog_increase_prcie.iv_cover
import kotlinx.android.synthetic.main.dialog_increase_prcie.tv_title
import kotlinx.android.synthetic.main.dialog_reply.*

import java.math.BigDecimal
import com.youth.xframe.utils.http.XHttp.handler
import androidx.core.os.HandlerCompat.postDelayed
import android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
import android.view.inputmethod.InputMethodManager


/**
 * 作者：SimGa
 * 日期：2020/2/26
 * 时间：11:04
 * 包名：com.ewhale.feitengguest.widget
 */
class ReplyDialog(context: Context) : Dialog(context, R.style.Dialog) {
    private val activity: MBaseActivity<*>
    private var position: Int? = null

    init {
        this.setCanceledOnTouchOutside(true)
        this.setCancelable(true)
        this.activity = context as MBaseActivity<*>
        setContentView(R.layout.dialog_reply)

        //解决dilaog中EditText无法弹出输入的问题
        window.clearFlags(FLAG_ALT_FOCUSABLE_IM)
        //弹出对话框后直接弹出键盘
        et_content.isFocusableInTouchMode = true
        et_content.requestFocus()
        et_content.postDelayed({
            val inputManager = et_content.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager.showSoftInput(et_content, 0)
        }, 100)
        btn_reply.setOnClickListener {
            if (et_content.text.isEmpty()) {
                activity.showToast("请输入回复内容")
                return@setOnClickListener
            }
            if (listenter != null) {
                listenter!!.onClickCommit(et_content.text.toString(), position!!)
            }
            et_content.setText("")
            dismiss()
        }
    }

    override fun onStart() {
        super.onStart()
        val window = window
        val dm = DisplayMetrics()
        window!!.windowManager.defaultDisplay.getMetrics(dm)
        window.setLayout(dm.widthPixels, window.attributes.height)
        window.setGravity(Gravity.BOTTOM)
        window.setWindowAnimations(R.style.bottom_int_out_dialog_style)
        window.setBackgroundDrawable(ColorDrawable(0x00000000))
    }

    private var listenter: onClickListenter? = null

    fun setListenter(listenter: onClickListenter) {
        this.listenter = listenter
    }

    fun replyPosition(position: Int) {
        this.position = position
    }

    interface onClickListenter {
        fun onClickCommit(content: String, position: Int)
    }
}
